// pages/dongTaiDetail/dongTaiDetail.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.url,
    dongtai: [],//动态详情
    datalist: [1, 1, 1],//详情底部  评论列表数据
    showmask: false,//是否显示弹框  true：显示   false：隐藏
    id:0,
    toUserId:0
  },
  //隐藏弹框
  hideMask: function (e) {
    this.setData({
      showmask: false
    })
  },
  //写评论按钮
  writeApparise: function (e) {
    this.setData({
      showmask: true,
      toUserId: e.currentTarget.dataset.to
    })
  },

  //事件处理函数
  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id
    })
    wx.request({
      url: app.globalData.url + '/index/workDynamic/' + options.id,
      responseType: 'text',
      success: function (res) {
        console.log(res);
        that.setData({
          dongtai: res.data.result,
        })
        console.log(that.data.dongtai)
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  reg: function (e) {
    var that = this;
    if (e.detail.value.content.length == 0) {
      wx.showToast({
        title: '内容不能为空!',
        icon: 'loading',
        duration: 500
      })
      return;
    }
    var toUserId =  that.data.toUserId;
    if (toUserId == undefined){
      toUserId = -1;
    }

    console.log(e.detail.value.content);
    console.log(that.data.id);
    console.log(toUserId);
      wx.request({
        url: app.globalData.url + '/comment/add',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: { content: e.detail.value.content, businessId: that.data.id, toUserId: toUserId},
        success: function (res) {
          console.log(res);
          if (res.data.status == "success") {
            wx.showToast({
              title: '保存成功',//这里打印出登录成功
              icon: 'success',
              duration: 1000
            })

            wx.navigateTo({
              url: '../dongTaiDetail/dongTaiDetail?id=' + that.data.id
            })
          } else if (res.data.status == "no_user") {
            wx.showModal({
              title: '提示',
              content: "请登录后参加考试",
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else {
                  console.log('用户点击取消')
                }
              }
            });
          }else{
            wx.showToast({
              title: res.data.result,
              icon: 'loading',
              duration: 1500
            })
          }
        }
      })
    }
})